<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.id)">查看</el-button>
      </template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-enabledCount="scope">
        {{ scope.row.enabledCount = 0? '0': scope.row.enabledCount }}
      </template>
      <template #table-noEnabledCount="scope">
        {{ scope.row.noEnabledCount = 0? '0': scope.row.noEnabledCount }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
      <template #footerBtn="data">
        <el-button type="primary" @click="doSelectDownload" icon="el-icon-s-home" size="mini">选择行下载</el-button>
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    doSelect(data) {
      this.selection = data
      console.log(data)
    },
    async doSelectDownload() {
      if (!this.selection || this.selection.length === 0) {
        this.$message.error('没有选择行')
        return
      }
      let dataList = []
      for(let i = 0; i < this.selection.length; i++) {
        dataList.push(this.selection[i])
      }
      try {
        await this.$confirm(`确定下载追溯码吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('download_trace_back_list', { dataList: dataList })
        if (!res.res) {
          return
        }
        this.$message.success(`批量下载完成!`)
      } finally {
        this.loading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoDetail(id = '') {
      let url = '/basic/traceabilityCodeDetail'
      if (id) {
        url += `?id=${ id }`
      }
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>

</style>
